package com.core.website.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.website.common.CommonRequest;
import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.entity.Banner;
import com.core.website.entity.Column;
import com.core.website.entity.Content;
import com.core.website.entity.User;
import com.core.website.service.AccessService;
import com.core.website.service.IpService;
import com.core.website.util.IpAddressUtil;
import com.core.website.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

@RestController
@RequestMapping("/indexApi")
public class IndexApi {
    @Autowired
    private ContentApi contentApi;
    @Autowired
    private BannerApi bannerApi;
    @Autowired
    private ColumnApi columnApi;
    @Autowired
    private IpService ipService;
    @Autowired
    private AccessService accessService;
    @PostMapping("/index")
    public Result index(@RequestBody List<CommonRequest> commonRequests, HttpServletRequest request) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        accessService.addDayAccess();
        //返回受限的栏目
        String ipAddress = IpAddressUtil.getIpAddr(request);
        Set<String> columnIdSet=new HashSet<>();
        if(StringUtils.isNotEmpty(ipAddress)){
            if(ipAddress.contains(",")){
                String[] split = ipAddress.split(",");
                for(String ip:split){
                    ipService.addStatisticalIpNum(ip);
                }
            }else{
                ipService.addStatisticalIpNum(ipAddress.trim());
            }
        }
        Map<String,Object> result=new HashMap<>();
        for(CommonRequest commonRequest:commonRequests){
            String type = commonRequest.getType();
            String cla = commonRequest.getCla();
            String method = commonRequest.getMethod();
            String param = JSON.toJSONString(commonRequest.getParam());
            Class c=null;
            Object api=null;
            Object p=null;
            if(contentApi.getClass().getSimpleName().equalsIgnoreCase(cla)){
                c=contentApi.getClass();
                api=contentApi;
                p=JSONObject.parseObject(param, Content.class);
            }else if(bannerApi.getClass().getSimpleName().equalsIgnoreCase(cla)){
                c=bannerApi.getClass();
                api=bannerApi;
                p=JSONObject.parseObject(param, Banner.class);
            }else if(columnApi.getClass().getSimpleName().equalsIgnoreCase(cla)){
                c=columnApi.getClass();
                api=columnApi;
                p=JSONObject.parseObject(param, Column.class);
            }
            Method classMethod=c.getMethod(method, p.getClass(),HttpServletRequest.class);
            Object invoke = classMethod.invoke(api,p,request);
            result.put(type,invoke);
        }
        return ResultUtil.success(result);
    }
    @GetMapping("/first")
    public Result first(HttpServletRequest request) {
        String firstSwich = ipService.getFirstSwich();
        if(StringUtils.isNotEmpty(firstSwich)&&"1".equals(firstSwich)){
            return ResultUtil.success(1);
        }
        String ipAddress = IpAddressUtil.getIpAddr(request);
        if(StringUtils.isNotEmpty(ipAddress)){
            ipService.first(ipAddress);
        }
        return ResultUtil.success(1);
    }
    @GetMapping("/checkFirst")
    public Result checkFirst(HttpServletRequest request) {
        String firstSwich = ipService.getFirstSwich();
        if(StringUtils.isNotEmpty(firstSwich)&&"1".equals(firstSwich)){
            return ResultUtil.success(0);
        }
        String ipAddress = IpAddressUtil.getIpAddr(request);
        if(StringUtils.isNotEmpty(ipAddress)){
            return ResultUtil.success(ipService.checkFirst(ipAddress));
        }else{
            return ResultUtil.success(1);
        }
    }

    @GetMapping("/getFirstSwich")
    public Result getFirstSwich() {
        return ResultUtil.success(ipService.getFirstSwich());
    }
    @GetMapping("/updateFirstSwich")
    public Result updateFirstSwich(String swich) {
        return ResultUtil.success(ipService.updateFirstSwich(swich));
    }

}
